package com.example.gameforum.controller;


import com.example.gameforum.entity.Comment;
import com.example.gameforum.entity.Post;
import com.example.gameforum.entity.User;
import com.example.gameforum.service.CommentService;
import com.example.gameforum.service.PostService;
import com.example.gameforum.utils.CommunityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class CommentController {

    @Autowired
    private PostService postService;


    @Autowired
    private CommentService commentService;



    @RequestMapping("/fapinglun/{id}")
    public String pingLun(HttpServletRequest request, Model model, @PathVariable("id")int id , String context){
        HttpSession session=request.getSession();
        Post post=postService.getById(id);
        User user= (User) session.getAttribute("user");
        Comment comment=new Comment();
        comment.setId(CommunityUtils.getRandomThreeNum());
        comment.setContext(context);
        comment.setTime(new Date());
        comment.setCpost(post);
        comment.setCuser(user);
        commentService.insertComment(comment);
        post=postService.getById(post.getId());
        model.addAttribute("post",post);
        return "post";
    }

    @RequestMapping("/pinglunuser")
    public String allPinglunByUser(HttpServletRequest request, Model model){
        HttpSession session=request.getSession();
        User user= (User) session.getAttribute("user");
        List<Comment> comments=commentService.getAllCommentsByUser(user);
        model.addAttribute("comments",comments);
        return "allcomments";
    }

}
